#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include "RingQueue.hpp"
struct threaddata
{
  RingQueue<int> *rq;
  std::string name;
};
void *consumer(void*args)
{
    //Blockqueue<task_t>*bq=static_cast<Blockqueue<task_t>*>(args);
    threaddata*td =static_cast<threaddata*>(args);
    while(1)
    {
        sleep(3);
        //1.消费任务
        int t=0;
        td->rq->Pop(&t);
        //2.处理任务
        std::cout<<td->name<<"消费者拿到了一个数据: "<<t<<std::endl;
    }
}
int data=1;
void * productor(void*args)
{
    threaddata*td =static_cast<threaddata*>(args);
    while(1)
    {
        sleep(3);
        std::cout<<td->name<<"生产了一个任务： "<<data<<std::endl;
        td->rq->Equeue(data);
        data++;
    }
}
int main()
{
    RingQueue<int >*rq=new RingQueue<int>();
    pthread_t c[2],p[3];
    threaddata*td1=new threaddata();
    td1->name="thread-1";
    td1->rq=rq;
    threaddata*td2=new threaddata();
    td2->name="thread-2";
    td2->rq=rq;
    threaddata*td3=new threaddata();
    td3->name="thread-3";
    td3->rq=rq;
    threaddata*td4=new threaddata();
    td4->name="thread-4";
    td4->rq=rq;
    threaddata*td5=new threaddata();
    td5->name="thread-5";
    td5->rq=rq;
     pthread_create(c, nullptr, consumer, td1);
    pthread_create(c+1, nullptr, consumer, td2);
    pthread_create(p, nullptr, productor, td3);
    pthread_create(p+1, nullptr, productor, td4);
    pthread_create(p+2, nullptr, productor, td5);

    pthread_join(c[0], nullptr);
    pthread_join(c[1], nullptr);
    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);
    pthread_join(p[2], nullptr);
    return 0;
}